package WorldOfTanks;

import java.util.Calendar;
import java.util.Random;

public class Vehicles {

	private int damage;
	private double damageBase = 1; // 主炮伤害基数，会受模块损坏与成员受伤的影响
	private int penetration; // 主炮穿透深度
	private double penetrationBase = 1;// 穿透力基数
	private int HP;
	private int armor; // 装甲
	private int reloadTime; // 装弹
	private double reloadBase = 1; // 装弹时间基数
	private boolean loading = false;
	private String name;
	private int speed; // 速度，影响躲避几率
	private double speedBase = 1; // 速度基数
	private double aim = 1; // 瞄准基数

	private String[] module = { "弹药架", "主炮", "引擎", "履带" };
	private boolean[] moduleDamaged = { false, false, false, false };
	private String[] crew = { "车长", "装填手", "炮手", "驾驶员" };
	private boolean[] crewDead = { false, false, false, false };

	public Vehicles(int damage, int penetration, int hP, int armor, int reloadTime, String name, int speed) {
		super();
		this.damage = damage;
		this.penetration = penetration;
		HP = hP;
		this.armor = armor;
		this.reloadTime = reloadTime;
		this.name = name;
		this.speed = speed;
	}

	public boolean isLoading() {
		return loading;
	}

	public void reload(int time) {
		loading = true;
		// System.out.println("开始装填");
		try {
			// Thread.sleep( (long) (1000*this.reloadBase*this.reloadTime) );
			Thread.sleep(1500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// System.out.println("装填完成");
		loading = false;
	}

	public void time(int time) {
		Calendar cal = Calendar.getInstance();
		long before = cal.getTimeInMillis();
		do {
			if (cal.getTimeInMillis() - before >= time * 1000)
				break;
		} while (true);
	}

	public void onFire() {

		if (HP < 400) {
			System.out.print("因燃烧血量减少" + HP);
			System.out.println("  " + this.getName() + "被烧毁");
			HP -= HP;
		} else {
			System.out.println("因燃烧血量减少" + 400);
			HP -= 400;
		}
	}

	public boolean isDestroyed() {
		if (this.HP <= 0)
			return true;
		else
			return false;
	}

	public void moduleAndCrew() {
		Random random = new Random();
		int r = Math.abs(random.nextInt(15));
		int rand = Math.abs(random.nextInt(2));
		if (r <= 3) {
			moduleDamaged[r] = true;
			switch (r) {
			case 0:
				if (rand == 0) {
					System.out.println("\n" + this.name + "的弹药架受损，装填时间增长");
					this.setReloadBase(2);
				} else {
					System.out.println("\n" + this.name + "的弹药殉爆，坦克已损毁！");
					HP = 0;
				}
				break;
			case 1:
				System.out.println("\n" + this.name + "主炮受损，瞄准精度、穿深与杀伤力削弱");
				this.setReloadBase(2);
				this.damageBase = 0.5;
				this.penetrationBase = 0.5;
				break;
			case 2:
				if (rand == 0) {
					System.out.println("\n" + this.name + "引擎工作不正常，速度下降");
					speedBase = 0.5;
				} else {
					System.out.println("\n" + this.name + "的引擎起火");
					this.onFire();

				}

				break;
			case 3:
				System.out.println("\n" + this.name + "履带被击中，无法移动");
				speedBase = 0;
				break;
			}
		}
		r = Math.abs(random.nextInt(15));
		if (r <= 3) {
			crewDead[r] = true;

			switch (r) {
			case 0:
				System.out.println("\n" + this.name + "的车长阵亡！各项属性削弱");
				setReloadBase(1.5);
				speedBase = 0.7;
				aim = 0.8;
				break;
			case 1:
				System.out.println("\n" + this.name + "的装填手阵亡！装弹时间增长");
				setReloadBase(2);
				break;
			case 2:
				System.out.println("\n" + this.name + "的炮手阵亡！瞄准精度下降");
				aim = 0.6;
				break;
			case 3:
				System.out.println("\n" + this.name + "的驾驶员阵亡！速度下降");
				speedBase = 0.4;
				break;
			}

		}

	}

	public boolean armorDefence(int penetration) {
		penetration += new Random().nextInt(40);
		if (penetration > armor)
			return false;
		else
			return true;

	}

	public int chooseTarget() {
		Random random = new Random();
		int target = random.nextInt(3);
		return target;
	}

	public void attack(Vehicles enemy) {
		// TODO Auto-generated method stub

		Random random = new Random();
		double miss = (enemy.speed * enemy.speedBase * 2) / 200 / aim;
		double mis = 0.3 + random.nextDouble();
		if (miss > mis) {
			System.out.println("但是没有击中目标");
			reload(reloadTime);
			return;
		}
		damage = (int) (damage * damageBase);
		penetration = (int) (penetration * penetrationBase);
		int r;
		r = random.nextInt(50) - 25;
		damage = (int) (damage * (double) (100 + r) / 100);
		enemy.beHit(this.damage);
		reload(reloadTime);
	}

	public void beHit(int damage) {
		// TODO Auto-generated method stub
		if (this.armorDefence(damage)) {
			Random random = new Random();
			int i = random.nextInt(2);
			if (i == 0)
				System.out.println("跳弹");
			else
				System.out.println("装甲未被击穿");
			return;
		}
		if (damage > HP)
			damage = HP;
		System.out.print(this.name + "HP减少" + damage);
		this.HP -= damage;
		moduleAndCrew();
		if (this.isDestroyed())
			System.out.println(this.getName() + "被击毁");
		else
			System.out.println("\n");

	}

	public int getHP() {
		return HP;
	}

	public void setHP(int hP) {
		HP = hP;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setLoading(boolean loading) {
		this.loading = loading;
	}

	public String[] getModule() {
		return module;
	}

	public void setModule(String[] module) {
		this.module = module;
	}

	public String[] getCrew() {
		return crew;
	}

	public void setCrew(String[] crew) {
		this.crew = crew;
	}

	public double getReloadBase() {
		return reloadBase;
	}

	public void setReloadBase(double reloadBase) {
		this.reloadBase = reloadBase;
	}

}